package cn.com.elite;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MTService extends HttpServlet{
	private static final Log log=LogFactory.getLog(MTService.class);
	public static String url1="http://debug.ct.test.sankuai.info/api/apply/ecom/import";
	public static String filename="";
	public static final String BOUNDARYSTR = "aifudao7816510d1hq";
	public static final String BOUNDARY = "--" + BOUNDARYSTR + "\r\n";

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
		this.doPost(request, response);
	}
	public void init(){
		url1=this.getInitParameter("url");
		filename=this.getInitParameter("filename");
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String charencoding=request.getCharacterEncoding();
		log.info("charencoding:"+charencoding);
		if(charencoding==null||charencoding.equals("")){
			request.setCharacterEncoding("UTF-8");
		}else{
			request.setCharacterEncoding(charencoding);
		}
		String dealNum=request.getParameter("dealNum");
		log.info("dealNum:"+dealNum);
		String reasons=request.getParameter("reasons");
		log.info("reasons:"+reasons);
		String detail=request.getParameter("detail");
		log.info("detail:"+detail);
		String contactName=request.getParameter("contactName");
		log.info("contactName:"+contactName);
		String contactPhone=request.getParameter("contactPhone");
		log.info("contactPhone:"+contactPhone);
		String submitUser=request.getParameter("submitUser");
		log.info("submitUser:"+submitUser);
		String submitUserId=request.getParameter("submitUserId");
		log.info("submitUserId:"+submitUserId);
		String urgent=request.getParameter("urgent");
		log.info("urgent:"+urgent);
		String path=request.getParameter("path");
		log.info("path:"+path);
		String filename1=filename+path;
		String str="";
		try{
			HttpClient client=new HttpClient();
	        client.setConnectionTimeout(8000);
	        PostMethod postMethod=new PostMethod(url1) {
	            public String getRequestCharSet() {
	                return "UTF-8";//
	            }
	        };
			StringPart normalParm1=new StringPart("dealNum",dealNum,"UTF-8");
			StringPart normalParm2=new StringPart("reasons",reasons,"UTF-8");
			StringPart normalParm3=new StringPart("detail",detail,"UTF-8");
			StringPart normalParm4=new StringPart("contactName",contactName,"UTF-8");
			StringPart normalParm5=new StringPart("contactPhone",contactPhone,"UTF-8");
			StringPart normalParm6=new StringPart("submitUser",submitUser,"UTF-8");
			StringPart normalParm7=new StringPart("submitUserId",submitUserId,"UTF-8");
			StringPart normalParm8=new StringPart("urgent",urgent,"UTF-8");
			FilePart file1=null;
			MultipartRequestEntity mrp=null;
            if(path!=null&&!path.equals("")&&!path.equals("null")){
            	file1=new EncodingFilePart("attachment",path,new File(filename1),null,"UTF-8");
            }
            if(file1!=null){
            	mrp=new MultipartRequestEntity(new Part[]{normalParm1,normalParm2,normalParm3,normalParm4,normalParm5,normalParm6,normalParm7,normalParm8,file1},postMethod.getParams());
            }else{
            	mrp=new MultipartRequestEntity(new Part[]{normalParm1,normalParm2,normalParm3,normalParm4,normalParm5,normalParm6,normalParm7,normalParm8},postMethod.getParams());
            }
            postMethod.getParams().setContentCharset( "UTF-8" );
            postMethod.setRequestEntity(mrp);
            int statusCode=client.executeMethod(postMethod);
            log.info("statusCode:"+statusCode);
            byte[] responseBody=postMethod.getResponseBody();
            response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
		    PrintWriter out=response.getWriter();;
			log.info("new String(responseBody)):"+new String(responseBody,"UTF-8"));
			out=response.getWriter();
			out.print(new String(responseBody,"UTF-8"));
            
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
	}

}